package com.yanqu.road.server.cmd.grandchild.born;

import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.entity.grandchild.GrandChildOneKeyResult;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.grandchild.GrandChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.grandson.GrandChildModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.pb.GrandChildPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.Set;

@Cmd(code = GameProtocol.S_HONEY_MOON_ONEKYE,desc = "一键度蜜月")
public class DoHoneyMoonOneKeyCmd extends AbstractCommand {
    private String id;
    private String childId;

    GrandChildOneKeyResult oneKeyResult;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GrandChildProto.HoneymoonOneKeyReqMsg req = GrandChildProto.HoneymoonOneKeyReqMsg.parseFrom(packet.getBytes());
        id = req.getId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(player.getUserInfo().getTitleId() < GameConfig.GRAND_CHILD_QUICK_UNLOCK_TALENT_LV && VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.GRAND_CHILD_QUICK_UNLOCK_TALENT_VIP){
            ret = GameErrorCode.E_GRAND_CHILD_ONEKEY_TALENT;
            return;
        }
         ret = player.getModule(GrandChildModule.class).checkDoHoneyMoonOneKey(id);
         if(ret == 0){
             childId = player.getModule(GrandChildModule.class).doHoneyMoonOneKey(id);
             GrandChildData grandChild = player.getModule(GrandChildModule.class).getGrandChild(childId);
             oneKeyResult = GrandChildConfigMgr.oneKeyActiveTalentList2(player,grandChild);
             ret = player.getModule(GrandChildModule.class).saveGrandChild(childId, grandChild.getNickName(), oneKeyResult.getChoiceList());
         }
    }

    @Override
    public void sendData(GamePlayer player) {
        GrandChildProto.HoneymoonOneKeyRespMsg.Builder resp = GrandChildProto.HoneymoonOneKeyRespMsg.newBuilder();
        resp.setRet(ret);
        if (ret == 0) {
            GrandChildData grandChild = player.getModule(GrandChildModule.class).getGrandChild(childId);
            GrandChildProto.GrandChildMsg.Builder builder = GrandChildPb.parseGrandChildMsg(grandChild);
            resp.setGrandChild(builder);
            Set<Integer> activeList = GrandChildConfigMgr.previewActiveTalentList(oneKeyResult.getOldChildData(),oneKeyResult.getChoiceList());
            resp.addAllActiveTalent(activeList);
            resp.setCost(oneKeyResult.getCost());
            log.info("玩家{}的一键孙子日志,孙子id{}",player.getUserId(),childId);
            log.info("初始所有技能:{}",oneKeyResult.getoOptionList().toString());
            log.info("最终所有技能:{}",oneKeyResult.getOptionList().toString());
            log.info("最终选择的技能:{}",oneKeyResult.getChoiceList().toString());
        }
        player.sendPacket(ClientProtocol.S_HONEY_MOON_ONEKYE, resp);
    }
}
